package com.example.fwx;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


public class LoginActivity extends AppCompatActivity {

    private EditText et_Username, et_PassWord;
    private Button btn_Login;
    private TextView tv_Register;
    CheckBox cb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setTitle("登录");
        cb = this.findViewById(R.id.cb);
        SharedPreferences sp = getSharedPreferences("app", MODE_PRIVATE);
        boolean check = sp.getBoolean("check", false);


        et_Username = this.findViewById(R.id.et_account);
        et_PassWord = this.findViewById(R.id.et_pwd);
        btn_Login = this.findViewById(R.id.btn_login);

        cb.setChecked(check);

        if (check) {
            String name = sp.getString("name", "");
            String pwd = sp.getString("pwd", "");
            et_Username.setText(name);
            et_PassWord.setText(pwd);
        }

        tv_Register = this.findViewById(R.id.tv_register);
        //用户名
        et_Username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    //当点击用户名输入框时，清空密码框
                    et_PassWord.setText("");
                } else {
                    String username = et_Username.getText().toString();
                    if (username.length() < 4) {
                        Toast.makeText(LoginActivity.this, "用户名长度必须大于4,请重新输入", Toast.LENGTH_SHORT).show();
                        et_Username.setText("");
                    }
                }
            }
        });
        //密码框
        et_PassWord.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {

                } else {
                    String password = et_PassWord.getText().toString();
                    if (password.length() < 6 || password.length() > 12) {
                        Toast.makeText(LoginActivity.this, "密码长度必须为6-12位,请重新输入", Toast.LENGTH_LONG).show();
                        et_PassWord.setText("");

                    }
                }
            }
        });
        DBHelper helper = new DBHelper(this);

        //登录事件监听
        btn_Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String username = et_Username.getText().toString();
                String password = et_PassWord.getText().toString();
                //判断输入是否为空，若为空，给出提示
                if (username.equals("") || password.equals("")) {
                    Toast.makeText(LoginActivity.this, "请输入账号或密码！", Toast.LENGTH_SHORT).show();
                    return;
                }
                //查询账号密码，若数据库表为空，提示用户注册
                Cursor query = helper.getReadableDatabase().query(DBHelper.TB_USER, null, "name=? and pwd=?", new String[]{username, password}, null, null, null);
                if (query.getCount() > 0) {
                    if (cb.isChecked()) {
                        sp.edit()
                                .putBoolean("check", cb.isChecked())
                                .putString("name", username)
                                .putString("pwd", password)
                                .commit();
                    }
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("username", username);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "用户名/密码错误", Toast.LENGTH_SHORT).show();
                }

            }


        });
        //注册监听事件，点击新用户，跳转到注册界面
        tv_Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }


}